package com.jamal2362.apktool.fragment.files;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.task.ExtractTask;
import com.jamal2362.apktool.task.IconTask;
import com.jamal2362.apktool.task.ImportFrameworkTask;
import com.jamal2362.apktool.util.MUtil;
import com.jamal2362.apktool.util.PopupUtils;
import com.jamal2362.apktool.util.TextUtil;
import com.jamal2362.apktool.view.RoundedBackgroundSpan;

public class ApplicationItem extends ErrorTree {
	private final boolean isSystem;

	public ApplicationItem(CharSequence msg, CharSequence details, boolean isSystem) {
		super(msg, details);
		this.isSystem = isSystem;
	}

	public void addApp(final PackageInfo pkg, final PackageManager pm) {
		final String appFullLabel = pkg.applicationInfo.loadLabel(pm) + "_v" + pkg.versionName + "(" + pkg.versionCode + ")";
		final String outApkName = TextUtil.replaceIllegal(appFullLabel);
		final String appSubLabel = pkg.packageName + ", v" + pkg.versionName + "(" + pkg.versionCode + ")";
		final File[] apks = MUtil.getApkFiles(pkg);
		Spannable splitSpan = new SpannableString(appSubLabel);
		if (apks.length > 1) {
			String split = getContext().getString(R.string.split);
			splitSpan = new SpannableString(" " + split + "  " + appSubLabel);
			RoundedBackgroundSpan round = new RoundedBackgroundSpan(Color.RED, Color.WHITE);
			splitSpan.setSpan(round, 0, split.length() + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			// splitSpan.setSpan(new BackgroundColorSpan(Color.RED), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		addChild(new ErrorTree(pkg.applicationInfo.loadLabel(pm), splitSpan) {
				@Override
				public void setIcon(ImageView icon) {
					try {
						Context ctx = icon.getContext();
						icon.setTag(apks[0].getAbsolutePath());
						File file = apks[0];
						IconTask itask = new IconTask(ctx, icon);
						itask.execute(file);
					} catch (Exception e) {
						icon.setImageDrawable(pkg.applicationInfo.loadIcon(pm));
					}
				}

				@Override
				public void click(View view) {
					PopupUtils.show(view, R.menu.app,
						new PopupUtils.Callback(){
							@Override
							public void call(Context ctx, int id) {
								File file = apks[0];
								switch (id) {
									case R.id.extract_app:
										try {
											if (apks.length > 1) {
												MUtil.extractApksDialog(ctx, apks, outApkName, appFullLabel);
											} else {
												new ExtractTask(ctx, outApkName + ".apk", null).execute(apks);
											}
										} catch (Exception e) {
											TextUtil.st(ctx, ctx.getString(R.string.errorf, e.getMessage()));
										}
										break;

									case R.id.app_details:
										MUtil.showAppDetails(ctx, null, pkg, pm);
										break;

									case R.id.import_framework:
										new ImportFrameworkTask(ctx).execute(file);
										break;

									case R.id.launchApp:
										MUtil.launchApp(ctx, pkg.packageName);
										break;

									case R.id.deleteApp:
										MUtil.deleteApp(ctx, pkg.packageName);
										break;

									default:
										MUtil.decode(ctx, null, apks, pkg.applicationInfo.loadLabel(pm).toString());
										break;
								}
							}
						});
				}
			});
	}
}
